/*
 * @Description:
 * @Author: mmzhang
 * @Date: 2021-12-22 15:05:19
 * @LastEditTime: 2022-03-31 10:59:04
 */
import dt from '@dt/dt'

function handle(opt, txt) {
  if (txt && opt) {
    if (opt.type == 'number') {
      txt = Number(txt).toFixed(opt.fixed == null || opt.fixed == undefined ? 2 : opt.fixed)
      txt = String(txt).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
      if (opt.unit) {
        txt += opt.unit
      }
    } else if (opt.type == 'dict') {
      txt = dt.dictInfo().__value(opt.key, txt)
    } else if (opt.type == 'date') {
      txt = txt.split(' ')[0]
    }
  }

  return txt !== null && txt !== '' && txt !== 'NaN' && txt !== undefined ? txt : '-'
}

function style(opt) {
  if (hasStyle(opt)) {
    return {
      color: opt.color,
      fontSize: opt.fontSize,
      fontWeight: opt.fontWeight,
      whiteSpace: opt.whiteSpace || 'nowrap',
      lineClamp: opt.lineClamp || '', //设置div的文本为2行
      webkitBoxOrient: opt.webkitBoxOrient || '',
      display: opt.display || 'block',
      wordWrap: opt.wordWrap,
      textAlign: opt.textAlign,
      height: opt.height,
      width: opt.width,
      float: opt.float,
      position: opt.position,
      left: opt.left,
      opacity: opt.opacity,
      lineHeight: opt.lineHeight,
      margin: opt.margin,
      backgroundColor: opt.backgroundColor
    }
  }
  return {
    whiteSpace: 'nowrap',
    display: 'block'
  }
}

function hasStyle(opt) {
  let has =
    opt.color ||
    opt.whiteSpace ||
    opt.lineClamp ||
    opt.webkitBoxOrient ||
    opt.display ||
    opt.wordWrap ||
    opt.show ||
    opt.textAlign ||
    opt.height ||
    opt.width ||
    opt.float ||
    opt.position ||
    opt.left ||
    opt.opacity ||
    opt.lineHeight ||
    opt.margin ||
    opt.backgroundColor
  return has
}

export default {
  handle,
  style,
  hasStyle
}
